﻿Public Class frmUser_Editor 
    Public is_edit As Boolean = False
    Public id As String

    Private Sub frmUser_Editor_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        If is_edit Then
            Me.Text = "Edit User"
            If mysql_isconnected() Then
                Dim data As DataTable
                data = mysql_query("SELECT * FROM tb_user WHERE id='" & id & "'")
                If data.Rows.Count > 0 Then
                    txtUsername.Text = data.Rows(0)("user")
                    txtUsername.Enabled = False
                    txtNick.Text = data.Rows(0)("nickname")
                Else
                    MessageBox.Show("Data Not Found. Please Try Again Later")
                    Me.Close()
                End If
            Else
                MessageBox.Show("Cannot Edit User Right Now. Please Try Again Later")
                Me.Close()
            End If
        Else
            Me.Text = "Add User"
        End If
    End Sub

    Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
        If is_edit Then
            If txtUsername.Text.Equals("") = False And txtNick.Text.Equals("") = False Then
                If txtcPassword.Text.Equals("") Or txtPassword.Equals("") Then
                    If mysql_isconnected() Then
                        mysql_non_query(String.Format("UPDATE tb_user SET user='{0}',nickname='{1}' WHERE id='{2}'", txtUsername.Text, txtNick.Text, id))
                        Me.Close()
                        frmUser.loadUser()
                    Else
                        MessageBox.Show("Can't Connect to Database Server")
                    End If
                Else
                    If txtPassword.Text.Equals(txtcPassword.Text) Then
                        If mysql_isconnected() Then
                            mysql_non_query(String.Format("UPDATE tb_user SET user='{0}',passw='{1}',nickname='{2}' WHERE id='{3}'", txtUsername.Text, txtPassword.Text, txtNick.Text, id))
                            Me.Close()
                            frmUser.loadUser()
                        Else
                            MessageBox.Show("Can't Connect to Database Server")
                        End If
                    Else
                        MessageBox.Show("Password and Confirmation Password not Match")
                    End If
                End If
                
            Else
                MessageBox.Show("Please fill all form correctly")
            End If
        Else
            If txtUsername.Text.Equals("") = False And txtPassword.Text.Equals("") = False And _
                txtcPassword.Text.Equals("") = False And txtNick.Text.Equals("") = False Then
                If txtPassword.Text.Equals(txtcPassword.Text) Then
                    If mysql_isconnected() Then
                        Dim data As DataTable
                        data = mysql_query("SELECT * FROM tb_user WHERE user='" & txtUsername.Text & "'")
                        If data.Rows.Count <= 0 Then
                            mysql_non_query(String.Format("INSERT INTO tb_user(user,passw,nickname) VALUES ('{0}','{1}','{2}')", txtUsername.Text, txtPassword.Text, txtNick.Text))
                            Me.Close()
                            frmUser.loadUser()
                        Else
                            MessageBox.Show("Email already exists")
                        End If
                    Else
                        MessageBox.Show("Can't Connect to Database Server")
                    End If
                Else
                    MessageBox.Show("Password and Confirmation Password not Match")
                End If
            Else
                MessageBox.Show("Please fill all form correctly")
            End If
        End If
        
    End Sub

    Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub
End Class